import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzUploadChangeParam } from 'ng-zorro-antd/upload';
import { UploadService } from 'src/app/services/upload.service';
import { ExceptionHandler } from 'src/app/handler/exception.handler';
@Component({
  selector: 'zepride-document',
  templateUrl: './document.component.html',
  styleUrls: ['./document.component.less']
})
export class DocumentComponent implements OnInit {

  constructor(
    public uploadService: UploadService,
    private msg: NzMessageService,
  ) { }
  isSpinning = false;
  filelist: any = [];
  changeUploadAction = '';
  url = '';
  arr = [];
  ngOnInit(): void {
    const orgid = '?orgid=' + localStorage.getItem('orgid');
    this.url = this.uploadService.uploadAction + orgid + '&ossDir=templateFile';
    this.changeUploadAction = this.uploadService.uploadAction + orgid + '&fileName=templateFile';
    this.getFilelist();
  }
  async getFilelist() {
    this.isSpinning = true;
    try {
      const { objects } = await this.uploadService.getFiles('templateFile');
      this.filelist = objects;
      if (!this.filelist) {
        this.isSpinning = false;
        return;
      }
      this.filelist = this.dataFilelist(this.filelist);
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
    this.isSpinning = false;

  }

  handleChangefile(info: NzUploadChangeParam, name?: string): void {

    this.isSpinning = true;
    if (info.file.status === 'done') {

      this.msg.success(`${info.file.name} 文件上传成功`);
      if (!name) {
        this.getFilelist();
      } else {

        this.filelist.forEach(item => {
          if (item.name == name) {
            item.name = info.file.name;
            item.url = info.file.response.fileUrl;
          }
        });
      }
      this.isSpinning = false;
    } else if (info.file.status === 'error') {
      this.isSpinning = false;
      this.msg.error(`${info.file.name} 文件上传失败`);
    }

  }


  async del(data) {
    this.isSpinning = true;
    try {
      await this.uploadService.delFiles('templateFile', data.name);
      this.filelist = this.filelist.filter(item => item !== data);
      this.msg.success('删除成功');
      this.getFilelist();
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
    this.isSpinning = false;

  }

  formatDateTime(date: any) {
    const time = new Date(Date.parse(date));
    time.setTime(time.setHours(time.getHours() + 8));
    const Y = time.getFullYear() + '.';
    const M = this.addZero(time.getMonth() + 1) + '.';
    const D = this.addZero(time.getDate()) + ' ';
    const h = this.addZero(time.getHours()) + ':';
    const m = this.addZero(time.getMinutes()) + ':';
    const s = this.addZero(time.getSeconds());
    date = new Date(Y + M + D + h + m + s).getTime();
    return date;
  }
  addZero(num: any) {
    return num < 10 ? '0' + num : num;
  }

  dataFilelist(data) {
    data.forEach(item => {
      item.lastModified = this.formatDateTime(item.lastModified);
      const index = item.name.lastIndexOf('/');
      item.name = item.name.substring(index + 1);
      this.arr.push(item.lastModified);
    });
    this.arr.sort((a, b) => b - a);
    const array = [];
    data.forEach((item, i) => {
      this.arr.forEach((item1, i1) => {
        if (item.lastModified == this.arr[i1]) {
          array[i1] = item;
        }
      });
    });
    this.arr = [];
    data = array;
    console.log(data);
    return data;

  }
}
